(function(){
	const $ = axios.create({
		baseURL: "http://127.0.0.1:8000",
		headers: {
			// "Content-Type": "multipart/form-data",
			// "Content-Type": "application/x-www-form-urlencoded"
		}
	})
	const upload = document.querySelector('.upload');
	// base64
	// upload.addEventListener('change', async function(res) {
	// 	const file = res.srcElement.files[0]
	// 	// console.log(res.srcElement.files[0]);
	// 	const base64 = await toBase64(file);
	// 	console.log(base64);
	// 	$.post('/upload_base64', {
	// 		file: encodeURIComponent(base64),
	// 		filename: file.name
	// 	}, {
	// 		// 原生 xhr 的 onprogress
	// 		onUploadProgress(e) {
	// 			console.log(e);
	// 			let { total, loaded } = e;
	// 			schedule.innerHTML = `${loaded / total * 100}%`;
	// 		}
	// 	})
	// });
	
	upload.addEventListener('change', async function(res) {
		const file = res.srcElement.files[0]
		fileSlice(file);
	});
	
	function toBase64(file) {
		return new Promise(resolve => {
			const fileReader = new FileReader();
			fileReader.readAsDataURL(file);
			// 异步的过程所以需要监听
			fileReader.onload = e => {
				// console.log(e.target.result);
				resolve(e.target.result);
			}
		})
	}
	
	// [固定数量 & 固定大小] 或者结合使用
	function fileSlice(file) {
		let fileSlices = [];
		let sliceMax = 1024 * 100; // 切片大小
		let count = Math.ceil(file.size / sliceMax); // 文件数量
		if (count > 100) {
			count = 100;
			sliceMax = file.size / count;
		}
		console.log(file);
		for (var i = 0; i < count; i++) {
			fileSlices.push({
				file: file.slice(i * sliceMax, (i + 1) * sliceMax),
				filename: `${file.name}_${i + 1}`
			});
		}
		console.log(fileSlices);
	}
})()